package com.mall.web.control.app.user;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.mall.biz.trade.TradeBizImpl;
import com.mall.biz.user.record.MallItemRecordBizImpl;
import com.mall.client.item.TableNumService;
import com.mall.client.order.MallItemOrderService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.MallItemOrder;
import com.mall.pojo.TableNum;
import com.mall.pojo.User;
import com.mall.query.Page;
import com.mall.query.TradeRecordQuery;
import com.mall.vo.item.MallItemOrderVO;
import com.mall.vo.item.MallItemVO;
import com.mall.web.annotation.UserSecurity;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.WebUtil;
/**
 * 用户的智慧餐厅记录
 * @author haiyang
 *
 */
@Controller
@RequestMapping("/user/zhihui/")
public class RecordMallItemController {
	@Autowired
	private MallItemRecordBizImpl mallItemRecordBiz;
	@Autowired
	private MallItemOrderService mallItemOrderService;
	@Autowired
	private TableNumService tableNumService;
	@Autowired
	private TradeBizImpl tradeBizImpl;
	
	/**
	 * 智慧餐厅订单记录
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("zhihuiRecord")
	public String zhihuiRecord(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeBizImpl.tradeRecordMap(request, response, modelMap);
		return "user/record/zhihuiRecord";
		
	}
	
	/**
	 * 加载更多
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	@UserSecurity
	@RequestMapping("zhihuiRecordList")
	public String zhihuiRecordList(HttpServletRequest request, HttpServletResponse response,ModelMap modelMap){
		tradeBizImpl.tradeRecordMap(request, response, modelMap);
		return "user/record/list/zhihuiRecordList";
		
	}
	
	/**
	 * 用户的智慧餐厅订单详情页
	 * @param request
	 * @param response
	 * @param modelMap
	 * @return
	 */
	
	@UserSecurity
	@RequestMapping("zhihuiRecordInfo")
	public String zhihuiInfo(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		Long mallItemOrderId = HttpUtil.getLongValueFromRequest(request, "mallItemOrderId");
		MallItemOrder mallItemOrder = this.mallItemOrderService.getMallItemOrderById(mallItemOrderId);
		String itemListJson = mallItemOrder.getBuyItemsJson();
		List<MallItemVO> waiMaiTradeDetailVOList = null;
		if (itemListJson != null) {
			waiMaiTradeDetailVOList = JSONArray.parseArray(itemListJson, MallItemVO.class);
		}
		TableNum tableNum = this.tableNumService.getTableNumById(mallItemOrder.getTableNumId());
		modelMap.put("mallItemOrder", mallItemOrder);
		modelMap.put("user", WebUtil.getCurrentUser(request));
		modelMap.put("tableNum", tableNum);

		modelMap.put("waiMaiTradeDetailVOList", waiMaiTradeDetailVOList);
		return "mall/record/recordinfo/zhihuiRecordInfo";
	}

//	private ModelMap tradeRecordMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
//		Page page = new Page();
//		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
//		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize",PageUtil.PAGE_SIZE_APP);
//		page.setPageByPageNoAndPageSize(pageNo, pageSize);
//		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
//		User user = WebUtil.getCurrentUser(request);
//		TradeRecordQuery tradeRecordQuery = new TradeRecordQuery();
//		tradeRecordQuery.setOnlyKey(onlyKey);
//		tradeRecordQuery.setUserId(user.getId());
//		tradeRecordQuery.setPage(page);
//		List<MallItemOrderVO> mallItemOrderVOList = this.mallItemRecordBiz.getMallItemOrderByQuery(tradeRecordQuery);
//		modelMap.put("mallItemOrderVOList", mallItemOrderVOList);
//		int app = APPCookieUtil.urlFromApp(request);
//		modelMap.put("app", app);
//		return modelMap;
//	}

}
